<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Youkuanswermodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'answer0' => 'answer0',
        'answer1' => 'answer1',
        'answer2' => 'answer2',
        'answer3' => 'answer3',
        'answer4' => 'answer4',
        'answer5' => 'answer5',
        'answer6' => 'answer6',
        'answer7' => 'answer7',
        'answer8' => 'answer8',
        'answer9' => 'answer9',
        'answer_time_create_start' => 'answer_time_create >=',
        'answer_time_create_end' => 'answer_time_create <=',
    );

    public static $likeField = array(
        'answer_result' => 'answer_result',
    );

    public static $sortField = array(
    );


    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getAnswerListSection($assist){
        if(!empty($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('youku_answer');
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        if(!empty($assist['where_in'])){
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    return array(array(),0);
                }
            }
        }
        if(!empty($assist['pagination'])) {
            $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);
        }

        if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('youku_answer.answer_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(empty($assist['pagination'])) {
            $count = count($list);
        }

        return array($list,$count);
    }


    public function getCustomerById($answer_id){
        if(!is_array($answer_id)) $answer_id = array($answer_id);
        if(empty($answer_id)) return array();

        $this->db->from('youku_answer');
        $this->db->where_in('youku_answer.answer_id',$answer_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }


    public function getCustomer($answer_id){
        $this->db->from('youku_answer');
        $this->db->where('youku_answer.answer_id',$answer_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $customer = $query->row_array();

        return $customer;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createAnswer($info){
        $db_debug = $this->db->db_debug;
        $this->db->db_debug = FALSE;
        $m = $this->db->insert('youku_answer',$info);
        $answer_id = $this->db->insert_id();
        $this->db->db_debug = $db_debug;

        return $m ? $answer_id : 0;
    }


/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editCustomer($info) {
        $this->db->where('answer_id',$info['answer_id']);
        unset($info['answer_id']);
        $this->db->update('youku_answer',$info);
    }

/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/

}
